<?php

/*

 This file is part of Gul 2 project.

 Gul project is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Gul project distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Gul project; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


$GulTagAllowed=array('STYLE' => 'xxx',
 'SCRIPT' => 'xxx',
 'WINDOW' => 'SCRIPT,STYLE,GROUPBOX,BOX,VBOX,HBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,SPLITTER,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'GROUPBOX' => 'SCRIPT,STYLE,CAPTION,SPLITTER,GROUPBOX,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'CAPTION' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'BOX' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'VBOX' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'HBOX' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'TABBOX' => 'SCRIPT,STYLE,TABS,TABPANELS',
 'STACK' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'DECK' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'GRID' => 'SCRIPT,STYLE,COLUMNS,ROWS',
 'COLUMNS' => 'SCRIPT,STYLE,COLUMN',
 'ROWS' => 'SCRIPT,STYLE,ROW',
 'COLUMN' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'ROW' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'TABS' => 'SCRIPT,STYLE,TAB',
 'TAB' => 'SCRIPT,STYLE,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'TABPANELS' => 'SCRIPT,STYLE,TABPANEL',
 'TABPANEL' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'BUTTON' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'LABEL' => 'SCRIPT,STYLE',
 'DESCRIPTION' => 'SCRIPT,STYLE',
 'SPACER' => 'SCRIPT,STYLE',
 'POPUPSET' => 'SCRIPT,STYLE,POPUP,MENUPOPUP',
 'POPUP' => 'SCRIPT,STYLE,MENUITEM,MENUSEPARATOR,MENU',
 'MENULIST' => 'SCRIPT,STYLE,MENUPOPUP,MENULIST',
 'MENUPOPUP' => 'SCRIPT,STYLE,MENUITEM,MENUSEPARATOR,MENU',
 'MENUITEM' => 'SCRIPT,STYLE',
 'MENU' => 'SCRIPT,STYLE,MENUPOPUP,MENULIST,POPUP',
 'MENUSEPARATOR' => 'SCRIPT,STYLE,',
 'TOOLTIP' => 'SCRIPT,STYLE,LABEL,DESCRIPTION',
 'TEXTBOX' => 'SCRIPT,STYLE',
 'CHECKBOX' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'RADIO' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'RADIOGROUP' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'LISTBOX' => 'SCRIPT,STYLE,LISTITEM,LISTCOLS,LISTHEAD',
 'LISTITEM' => 'SCRIPT,STYLE,LISTCELL',
 'LISTCOLS' => 'SCRIPT,STYLE,LISTCOL',
 'LISTCOL' => 'SCRIPT,STYLE',
 'LISTCELL' => 'SCRIPT,STYLE',
 'LISTHEADER' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'LISTHEAD' => 'SCRIPT,STYLE,LISTHEADER',
 'COLORPICKER' => 'SCRIPT,STYLE',
 'PROGRESSMETER' => 'SCRIPT,STYLE',
 'SCALE' => 'SCRIPT,STYLE',
 'BBOX' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'STATUSBAR' => 'SCRIPT,STYLE,PROGRESSMETER',
 'TREE' => 'SCRIPT,STYLE,TREECOLS,TREECHILDREN',
 'TREECOLS' => 'SCRIPT,STYLE,TREECOL',
 'TREECOL' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID',
 'TREECHILDREN' => 'SCRIPT,STYLE,TREEITEM',
 'TREEITEM' => 'SCRIPT,STYLE,TREEROW,TREECHILDREN',
 'TREEROW' => 'SCRIPT,STYLE,TREECELL',
 'TREECELL' => 'SCRIPT,STYLE',
 'IMAGE' => 'SCRIPT,STYLE',
 'SPLITTER' => 'SCRIPT,STYLE,GRIPPY',
 'GRIPPY' => 'SCRIPT,STYLE',
 'SEPARATOR' => 'SCRIPT,STYLE',
 'MENUBAR' => 'SCRIPT,STYLE,MENU',
 'TOOLBOX' => 'SCRIPT,STYLE,SPLITTER,BOX,VBOX,HBOX,GROUPBOX,STACK,MENUPOPUP,MENULIST,TABBOX,BUTTON,LABEL,DESCRIPTION,SPACER,POPUPSET,TOOLTIP,TEXTBOX,CHECKBOX,RADIO,RADIOGROUP,LISTBOX,COLORPICKER,PROGRESSMETER,SCALE,BBOX,STATUSBAR,IMAGE,SEPARATOR,DECK,MENUBAR,TOOLBOX,TOOLBAR,GRID,TREE',
 'TOOLBAR' => 'SCRIPT,STYLE,MENU,TOOLBARBUTTON,TOOLBARSEPARATOR',
 'TOOLBARSEPARATOR' => 'SCRIPT,STYLE',
 'TOOLBARBUTTON' => 'SCRIPT,STYLE',
 'MENULISTPOPUP' => 'MENUITEM',
 'NOTHING' => 'xxx');

 // note : MENULISTPOPUP & NOTHING doesn't exist, this is internal tag of Gul



function GulTagAllowed($tag)
{
 global $GulTagAllowed;

 $r=0;
 if($GulTagAllowed[$tag]) $r=1;

 return($r);
}



function GulTagChildAllowed($tag)
{
 global $GulTagAllowed,$Xwidgetsn,$Yn;

 $r=0;
 
 if($Xwidgetsn[$Yn-1])
 {
  if(strstr(','.$GulTagAllowed[$Xwidgetsn[$Yn-1]].',',','.$tag.',')) $r=1;
 }
 else $r=1;

 return($r);
}



function GullKillError($msg,$died=1)
{
 global $GulLastErr;

 if($died) die("\n".$msg."\n");
 else if(!$GulLastErr) $GulLastErr=$msg;
}







function GulDebugFile($file)
{
 if (!($fp = fopen($file, "r"))) GullKillError('Gul Parser error : '.$file.' not found !');
 return(GulDebug(fread($fp, filesize($file))));
}


function GulDebug($xul)
{
 global $GulDieMode,$Yn,$Xn,$Xwidgets,$Xlast,$Guls,$GulLastErr,$xml_parser;

 $Yn=0;
 $Xn='';
 $Xwidgets='';
 $Xlast=array();
 $Xlast[lastfontsize]=12;
 $GulLastErr=0;

 $actual=count($Guls);

 $xml_parser=xml_parser_create();
 xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
 xml_set_character_data_handler($xml_parser, 'XMLXuldebugHandler');
 xml_set_element_handler($xml_parser, 'XMLparseXuldebugDebutElement', 'XMLparseXuldebugFinElement');
 xml_set_processing_instruction_handler($xml_parser,'XMLparseXuldebugCodeElement');
 if(!xml_parse($xml_parser,$xul)) GullKillError('Gul XML error : '.xml_error_string(xml_get_error_code($xml_parser)).' on line '.xml_get_current_line_number($xml_parser).' !',0);

 return($GulLastErr);
 $GulLastErr='';
}


function XMLParseXuldebugDebutElement($parser,$name,$attrs)
{
 global $Yn,$Xwidgetsn,$GulTagAllowed,$xml_parser;

 $Xhandler='';
 $Yn++;

 // debug
 if(!GulTagAllowed($name)) GullKillError('GUL XML error : <'.$name.'> not allowed on line '.xml_get_current_line_number($xml_parser).' !',0);
 if(!GulTagChildAllowed($name)) GullKillError('GUL XML error : <'.$name.'> not allowed in <'.$Xwidgetsn[$Yn-1].'> on line '.xml_get_current_line_number($xml_parser).' !',0);
 
 $Xwidgetsn[$Yn]=$name;
}


function XMLXuldebugHandler($parser,$data)
{
 /* nothing */
}


function XMLparseXuldebugFinElement($parser, $name)
{
 global $Yn,$GulTagAllowed;

 if($GulTagAllowed[$name]) $Yn--;
}


function XMLparseXuldebugCodeElement($parser, $name, $data)
{
 /* nothing */
}



?>
